package com.zondy.mapgis.psmap.manager.controller;

import cn.hutool.core.io.IoUtil;
import com.zondy.mapgis.common.controllerprefix.annotation.ManagerRestController;
import com.zondy.mapgis.common.core.web.controller.BaseController;
import com.zondy.mapgis.common.core.web.domain.AjaxResult;
import com.zondy.mapgis.common.core.web.page.TableDataInfo;
import com.zondy.mapgis.common.log.annotation.Log;
import com.zondy.mapgis.common.log.enums.BusinessType;
import com.zondy.mapgis.common.security.annotation.RequiresPermissions;
import com.zondy.mapgis.common.security.utils.SecurityUtils;
import com.zondy.mapgis.domain.psmap.AppWidget;
import com.zondy.mapgis.psmap.manager.service.IAppWidgetService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.multipart.MultipartFile;

@Tag(
   name = "微件管理",
   description = "微件控制器"
)
@ManagerRestController({"/app/widget"})
public class AppWidgetController extends BaseController {
   @Autowired
   private IAppWidgetService appWidgetService;

   @Operation(
      summary = "查询微件列表"
   )
   @PreAuthorize("@ss.hasPermi('app:widget:list')")
   @RequiresPermissions({"app:widget:list"})
   @GetMapping({"/list"})
   public TableDataInfo list(AppWidget appWidget) {
      this.startPage();
      List<AppWidget> list = this.appWidgetService.selectAppWidgetList(appWidget);
      return this.getDataTable(list);
   }

   @Operation(
      summary = "导入微件列表"
   )
   @PreAuthorize("@ss.hasPermi('app:widget:import')")
   @RequiresPermissions({"app:widget:import"})
   @Log(
      title = "微件管理",
      businessType = BusinessType.IMPORT
   )
   @PostMapping({"/importData"})
   public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
      List<AppWidget> appWidgetList = this.appWidgetService.parseWidgetsFromFile(file, updateSupport);
      String operName = SecurityUtils.getUsername();
      String message = this.appWidgetService.importWidgets(appWidgetList, updateSupport, operName);
      return this.success(message);
   }

   @Operation(
      summary = "下载导入模板"
   )
   @PostMapping({"/importTemplate"})
   public void importTemplate(HttpServletResponse response) {
   }

   @Operation(
      summary = "导出微件列表"
   )
   @PreAuthorize("@ss.hasPermi('app:widget:export')")
   @RequiresPermissions({"app:widget:export"})
   @Log(
      title = "微件管理",
      businessType = BusinessType.EXPORT
   )
   @PostMapping({"/export"})
   public void export(HttpServletResponse response, AppWidget appWidget) throws IOException {
      List<AppWidget> list = this.appWidgetService.selectAppWidgetList(appWidget);
      byte[] data = this.appWidgetService.exportWidgets(list);
      this.genCode(response, data);
   }

   @Operation(
      summary = "获取微件详细信息"
   )
   @PreAuthorize("@ss.hasPermi('app:widget:query')")
   @RequiresPermissions({"app:widget:query"})
   @GetMapping({"/{widgetId}"})
   public AjaxResult getInfo(@PathVariable("widgetId") Long widgetId) {
      return this.success(this.appWidgetService.selectAppWidgetByWidgetId(widgetId));
   }

   @Operation(
      summary = "新增微件"
   )
   @PreAuthorize("@ss.hasPermi('app:widget:add')")
   @RequiresPermissions({"app:widget:add"})
   @Log(
      title = "微件管理",
      businessType = BusinessType.INSERT
   )
   @PostMapping
   public AjaxResult add(@RequestBody AppWidget appWidget) {
      if (!this.appWidgetService.checkWidgetNameUnique(appWidget.getWidgetName())) {
         return this.error("新增微件'" + appWidget.getWidgetName() + "'失败，微件名称已存在");
      } else {
         appWidget.setCreateBy(SecurityUtils.getUsername());
         return this.toAjax(this.appWidgetService.insertAppWidget(appWidget));
      }
   }

   @Operation(
      summary = "修改微件"
   )
   @PreAuthorize("@ss.hasPermi('app:widget:edit')")
   @RequiresPermissions({"app:widget:edit"})
   @Log(
      title = "微件管理",
      businessType = BusinessType.UPDATE
   )
   @PutMapping
   public AjaxResult edit(@RequestBody AppWidget appWidget) {
      appWidget.setUpdateBy(SecurityUtils.getUsername());
      return this.toAjax(this.appWidgetService.updateAppWidget(appWidget));
   }

   @Operation(
      summary = "删除微件"
   )
   @PreAuthorize("@ss.hasPermi('app:widget:remove')")
   @RequiresPermissions({"app:widget:remove"})
   @Log(
      title = "微件管理",
      businessType = BusinessType.DELETE
   )
   @DeleteMapping({"/{widgetIds}"})
   public AjaxResult remove(@PathVariable Long[] widgetIds) {
      return this.toAjax(this.appWidgetService.deleteAppWidgetByWidgetIds(widgetIds));
   }

   private void genCode(HttpServletResponse response, byte[] data) throws IOException {
      response.reset();
      response.addHeader("Access-Control-Allow-Origin", "*");
      response.addHeader("Access-Control-Expose-Headers", "Content-Disposition");
      response.setHeader("Content-Disposition", "attachment; filename=\"mapgis.zip\"");
      response.addHeader("Content-Length", "" + data.length);
      response.setContentType("application/octet-stream; charset=UTF-8");
      IoUtil.write(response.getOutputStream(), false, data);
   }
}
